package com.jinniulive.platform.module.login;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jinniulive.platform.R;
import com.jinniulive.platform.bean.ModifyPersonalInfo;
import com.jinniulive.platform.urls.NetConfig;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;


public class NameSettingActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText mEditTextName;
    private TextView mTextViewSave;
    private String tokenID;
    private String userID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_name_setting);
        String name = getIntent().getStringExtra("name");
        tokenID = getIntent().getStringExtra("tokenID");
        userID = getIntent().getStringExtra("userID");
        initView(name);
        setListener();


    }

    private void setListener() {
        mTextViewSave.setOnClickListener(this);
    }

    private void initView(String name) {
        mEditTextName = (EditText) findViewById(R.id.activity_name_setting_et_name);
        mTextViewSave = (TextView) findViewById(R.id.activity_namee_setting_tv_save);
        mEditTextName.setText(name);
    }

    @Override
    public void onClick(View v) {
        if (v != null) {
            switch (v.getId()){
                case R.id.activity_name_setting_bt_back:
                    finish();
                    break;
                case R.id.activity_namee_setting_tv_save:
                    String trim = mEditTextName.getText().toString().trim();
                    if (trim ==null || trim.isEmpty()){
                        Toast.makeText(this, "名字不能为空", Toast.LENGTH_SHORT).show();
                    }else{
                        try {
                            String encode = URLEncoder.encode(trim, "UTF-8");
                            String format = String.format(NetConfig.XGNC, tokenID, userID, encode);
                            RequestParams params= new RequestParams(format);
                            x.http().get(params, new Callback.CommonCallback<String>() {
                                @Override
                                public void onSuccess(String result) {
                                    Gson gson =new Gson();
                                    ModifyPersonalInfo info = gson.fromJson(result, ModifyPersonalInfo.class);
                                    int errorcode = info.getErrorcode();
                                    if (errorcode == 200){
                                        Toast.makeText(NameSettingActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                                        finish();
                                    }
                                }

                                @Override
                                public void onError(Throwable ex, boolean isOnCallback) {

                                }

                                @Override
                                public void onCancelled(CancelledException cex) {

                                }

                                @Override
                                public void onFinished() {

                                }
                            });
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }

                    }
                    break;
                default:
                    break;
            }
        }
    }
}
